package com.zte.cl.parser;

import java.util.List;

import org.apache.log4j.Logger;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import com.alibaba.fastjson.JSON;
import com.zte.cl.model.News;
import com.zte.cl.entity.Expr;
import com.zte.cl.entity.Field;
import com.zte.cl.entity.PageTemplate;
import com.zte.cl.parser.enums.PageType;
import com.zte.cl.parser.utils.SelectableUtil;
import com.zte.cl.service.NewsQueue;
import com.zte.cl.utils.ConstantUtil;
import com.zte.cl.utils.ExtractUtil;

/**
 * 内容页解析器 PageProcessor负责解析页面，抽取有用信息，以及发现新的链接
 * 
 * @author Administrator
 * 
 */
public class ContentPageProcessor implements PageProcessor {

	private final static Logger log = Logger.getLogger(ContentPageProcessor.class);

	private Site site;
	private News news;

	@Override
	public void process(Page page) {
		log.info("正在处理页面 pageUrl=" + news.getPageUrl());
		String url = page.getRequest().getUrl();
		PageTemplate wp = PageTemplateUtil.findPageTemplateByUrlPrefix(url, PageType.contentPage.toString()); // 内容页面模板
		if (wp != null) {
			String filteredContent = "";
			List<Expr> conExprs = wp.getExprs();
			try {
				if (conExprs != null && conExprs.size() > 0) {// 第一层处理
					Selectable conSt = SelectableUtil.getSelectable(page.getHtml(), conExprs);
					filteredContent = conSt.get();
				} else {
					filteredContent = page.getHtml().toString();
				}
			} catch (Exception e) {
				log.error(news.toString(), e);
				// 模板没找到，重新把任务放入队列中
				if (news.getFailNum() < ConstantUtil.CRAW_CONTENT_MAX_FAILTIME) {
					NewsQueue.push(news);
				}
				return;
			}
			List<Field> fields = wp.getFields();
			boolean check = ExtractUtil.extractToNews(filteredContent, fields, news);
			
			if (check && news!=null) {
				page.putField("news", news);
				page.putField("orig_page_html", page.getHtml());// 原始网页
			} else {
				log.warn("此条news无效!newsId:" + news.getId() + ", url:" + news.getPageUrl());
			}
		} else {
			log.error("没有获取到文章内容，请检查模板," + JSON.toJSONString(news));
			// 模板没找到，重新把任务放入队列中
			if (news.getFailNum() < ConstantUtil.CRAW_CONTENT_MAX_FAILTIME) {
				news.setFailNum(news.getFailNum()+1);
				NewsQueue.push(news);
			}
		}
	}

	public ContentPageProcessor(Site site, News news) {
		this.site = site;
		this.news = news;
	}

	@Override
	public Site getSite() {
		if (this.site == null) {
			this.site = Site.me().setSleepTime(ConstantUtil.SITE_SLEEP_TIME).setUserAgent(ConstantUtil.SITE_USER_AGENT);
		}
		return this.site;
	}
}
